/****************************************************************************
**
** Profession.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
TProfession::TProfession()
{
	m_Parent = NULL;

	m_DefName = "";
	m_Name = "";
	m_Type = false;
	m_Gump = 0;

	m_ScrollHeight = 0.0f;
	
	memset(m_SkillID, 0, sizeof(m_SkillID));
	memset(m_SkillValue, 0, sizeof(m_SkillValue));

	m_Str = m_Int = m_Dex = 0;

	m_Description = "";
	
	m_TextureName.Width = 0;
	m_TextureDescription.Width = 0;
}
//---------------------------------------------------------------------------
TProfession::~TProfession()
{
	if (m_TextureName.Width) glDeleteTextures(1, &m_TextureName.Texture);
	if (m_TextureDescription.Width) glDeleteTextures(1, &m_TextureDescription.Texture);
}
//---------------------------------------------------------------------------
void TProfession::SetName(string val)
{
	m_Name = val;
	//if (!UO->GenerateTextHeight(9, m_TextureName, m_Name.c_str(), m_Name.length(), 0x1, UO->GetTextWidth(9, m_Name.c_str(), m_Name.length()))) {TPRINT("GTH->N FAILED\n");}
	String str = String(val).ToUnicode();
	if (!UO->m_Font->GenerateW(1, m_TextureName, str.w_str(), 0, 30, 185.0f)) {TPRINT("GTH->N FAILED\n");}
}
//---------------------------------------------------------------------------
bool TProfession::AddDescription(string name, const char *val, int Height)
{
	if (m_Description.length()) return false;
	char buf[50] = {0};
	strncpy(buf, m_Name.c_str(), m_Name.length());
	string cmp(strlwr(buf));
	if (cmp == name)
	{
		m_ScrollHeight = (float)Height;
		m_Description = val;
		//if (!UO->GenerateTextHeight(9, m_TextureDescription, m_Description.c_str(), m_Description.length(), 0x1, 185)) {TPRINT("GTH->D FAILED\n");}
		String str = String(val).ToUnicode();
		if (!UO->m_Font->GenerateW(1, m_TextureDescription, str.w_str(), 0, 30, 185.0f)) {TPRINT("GTH->N FAILED\n");}
		return true;
	}
	return false;
}
//---------------------------------------------------------------------------
TProfessionCategory::TProfessionCategory()
{
	m_Parent = NULL;

	m_CatChildCount = 0;
	m_ProfChildCount = 0;

	m_ScrollHeight = 0.0f;
	
	m_CatChild[0] = NULL;
	m_CatChild[1] = NULL;
	m_CatChild[2] = NULL;
	m_CatChild[3] = NULL;
	
	m_ProfChild[0] = NULL;
	m_ProfChild[1] = NULL;
	m_ProfChild[2] = NULL;
	m_ProfChild[3] = NULL;
	
	Childs[0] = "";
	Childs[1] = "";
	Childs[2] = "";
	Childs[3] = "";

	m_DefName = "";
	m_Name = "";
	m_Type = false;
	m_Gump = 0;
	
	m_Description = "";
	
	m_TextureName.Width = 0;
	m_TextureDescription.Width = 0;
}
//---------------------------------------------------------------------------
TProfessionCategory::~TProfessionCategory()
{
	if (m_CatChildCount > 0)
	{
		for (int i = 0; i < m_CatChildCount; i++)
		{
			delete m_CatChild[i];
			m_CatChild[i] = NULL;
		}
	}
	if (m_ProfChildCount > 0)
	{
		for (int i = 0; i < m_ProfChildCount; i++)
		{
			delete m_ProfChild[i];
			m_ProfChild[i] = NULL;
		}
	}
	if (m_TextureName.Width) glDeleteTextures(1, &m_TextureName.Texture);
	if (m_TextureDescription.Width) glDeleteTextures(1, &m_TextureDescription.Texture);
}
//---------------------------------------------------------------------------
void TProfessionCategory::SetName(string val)
{
	m_Name = val;
	//if (!UO->GenerateTextHeight(9, m_TextureName, m_Name.c_str(), m_Name.length(), 0x1, UO->GetTextWidth(9, m_Name.c_str(), m_Name.length()))) {TPRINT("GTH->N FAILED\n");}
	String str = String(val).ToUnicode();
	if (!UO->m_Font->GenerateW(1, m_TextureName, str.w_str(), 0, 30, 185.0f)) {TPRINT("GTH->N FAILED\n");}
}
//---------------------------------------------------------------------------
bool TProfessionCategory::AddCategory(TProfessionCategory *pc, bool TopObj)
{
	if (TopObj && m_CatChildCount <= 3)
	{
		pc->m_Parent = this;
		m_CatChild[m_CatChildCount] = pc;
		m_CatChildCount++;
		return true;
	}
	if (m_CatChildCount < 4)
	{
		for (int i = 0; i < 4; i++)
		{
			if (Childs[i] == pc->GetDefName())
			{
				pc->m_Parent = this;
				m_CatChild[m_CatChildCount] = pc;
				m_CatChildCount++;
				return true;
			}
		}
	}
	if (m_CatChildCount > 0)
	{
		for (int i = 0; i < m_CatChildCount; i++)
		{
			pc->m_Parent = this;
			if (m_CatChild[i]->AddCategory(pc)) return true;
		}
	}
	return false;
}
//---------------------------------------------------------------------------
bool TProfessionCategory::Add(TProfession *p)
{
	if (m_CatChildCount > 0)
	{
		for (int i = 0; i < m_CatChildCount; i++)
		{
			if (m_CatChild[i]->Add(p)) return true;
		}
	}
	if (m_ProfChildCount < 4)
	{
		for (int i = 0; i < 4; i++)
		{
			if (Childs[i] == p->GetDefName())
			{
				p->m_Parent = this;
				m_ProfChild[m_ProfChildCount] = p;
				m_ProfChildCount++;
				return true;
			}
		}
	}
	return false;
}
//---------------------------------------------------------------------------
void TProfessionCategory::AddChildren(string children)
{
	for (int i = 0; i < 4; i++)
	{
		if (Childs[i] == string(""))
		{
			Childs[i] = children;
			break;
		}
	}
}
//---------------------------------------------------------------------------
bool TProfessionCategory::AddDescription(string name, const char *val, int Height)
{
	if (!m_Description.length())
	{
		char buf[50] = {0};
		strncpy(buf, m_Name.c_str(), m_Name.length());
		string cmp(strlwr(buf));
		if (cmp == name)
		{
			m_ScrollHeight = (float)Height;
			m_Description = val;
			//if (!UO->GenerateTextHeight(9, m_TextureDescription, m_Description.c_str(), m_Description.length(), 0x1, 185)) {TPRINT("GTH->D FAILED\n");}
			String str = String(val).ToUnicode();
			if (!UO->m_Font->GenerateW(1, m_TextureDescription, str.w_str(), 0, 30, 185.0f)) {TPRINT("GTH->N FAILED\n");}
			return true;
		}
	}
	if (m_CatChildCount > 0)
	{
		for (int i = 0; i < m_CatChildCount; i++)
		{
			if (m_CatChild[i]->AddDescription(name, val, Height)) return true;
		}
	}
	if (m_ProfChildCount > 0 )
	{
		for (int i = 0; i < m_ProfChildCount; i++)
		{
			if (m_ProfChild[i]->AddDescription(name, val, Height)) return true;
		}
	}
	return false;
}
//---------------------------------------------------------------------------